<?php

namespace App\Console\Commands;

use App\Model\Branch;
use App\Model\BranchTest;
use App\Model\Order;
use App\Model\Order1;
use App\Model\PushOrder;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class fywPush extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fywPush';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while(true){
            try {
                /*真实推单*/
                //控制是否推单给下线
                $branch = Branch::where("id", 8)->first();
//            $branch = BranchTest::where("id",8)->first();
                if ($branch['tel'] == 1) {
                    $order = DB::select("select id,buytime,chargeaccount,remainingnumber,price,num,orderno from tiktok_pushorder where status = '未处理' order by id asc limit 1");
//                $order = DB::select("select id,buytime,chargeaccount,remainingnumber,price,num,orderno from tiktok_order1 where status = '未处理'   order by id asc limit 1");
                    $num = Branch::where("tel", 1)->count();
                    if (isset($order[0])) {
                        $order1 = $order[0];
                        $order = (array)$order1;
                        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                        if ($order['id'] % 1 == 0) {
                            $res = $this->fywPush($order['orderno'], $order['chargeaccount'], $order['remainingnumber']);
                            $info['ext'] = $res;
                            PushOrder::where("orderno", $order['orderno'])->update($info);
//                        Order1::where("orderno",$order['orderno'])->update($info);
                            $res = json_decode($res);
                            if ($res->code == 0) {
                                $info['status'] = '已处理';
                                $info['branch'] = '已推送,等待反馈';
                                $info['isok'] = 3;
                                $info['bid'] = 8;
                                $info['time'] = time();
                                PushOrder::where('orderno', $order['orderno'])->update($info);
                                Order::where('orderno', $order['orderno'])->update($info);
//                            Order1::where('orderno', $order['orderno'])->update($info);
                                $branch = Branch::where("id", 8)->first();
                                $orderInfo['num'] = $branch['num'] + 1;
                                $orderInfo['money'] = $branch['money'] + $order['remainingnumber'] * $order['num'];
                                Branch::where("id", $branch['id'])->update($orderInfo);
                                echo isset($order['id']) ? $order['id'] : 0;
                            }
                        }
                    }
                }
                /*真实推单end*/
                /*虚拟推单*/
                //控制是否推单给下线
//            $branch = BranchTest::where("id",15)->first();
//                if ($branch['tel'] == 1) {
//                $order = DB::select("select id,buytime,chargeaccount,remainingnumber,price,num,orderno from tiktok_order1 where status = '未处理'   order by id asc limit 1");
//                    $num = Branch::where("tel", 1)->count();
//                    if (isset($order[0])) {
//                        $order1 = $order[0];
//                        $order = (array)$order1;
//                        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//                        if ($order['id'] % 1 == 0) {
//                            $res = $this->fivePush($order['orderno'], $order['chargeaccount'], $order['remainingnumber']);
//                            $info['ext'] = $res;
//                        Order1::where("orderno",$order['orderno'])->update($info);
//                            $res = json_decode($res);
//                            if ($res->code == 200) {
//                                $info['status'] = '已处理';
//                                $info['branch'] = '已推送,等待反馈';
//                                $info['isok'] = 3;
//                                $info['bid'] = 15;
//                                $info['time'] = time();
//                            Order1::where('orderno', $order['orderno'])->update($info);
//                                $branch = Branch::where("id", 15)->first();
//                                $orderInfo['num'] = $branch['num'] + 1;
//                                $orderInfo['money'] = $branch['money'] + $order['remainingnumber'] * $order['num'];
//                                Branch::where("id", $branch['id'])->update($orderInfo);
//                                echo isset($order['id']) ? $order['id'] : 0;
//                            }
//                        }
//                    }
//                }
                /*虚拟推单end*/
            } catch (\Exception $e) {
                echo $e->getMessage();
            }
            sleep(2);
        }
    }

    //推送方法
    public function fywPush($customer_order_no,$charge_account,$money){
        $url = "http://154.221.17.142:9980/api/open/addSupplyNumber";
        $callBackUrl = 'http://112.124.24.221/admin/callback/fywAutoConfirmOrder';
        $key = "BUUTL3VOIGRIK9SMF0YAZRIGZS5P7IW9HBAUMUQSXDH758RURGRVPJ4UQOZGL2S8DX4N6IUCSIMF10CSPO7MR7GAF2BIMWHVGGIECMCE7CTEQJNKQMD58BLG7CGWIST9";
        $money = number_format($money,2);
        $data = [
            "amount"      =>      $money*100,
            "mchId"             =>      567464,
            "mchOrderNo"         =>      $customer_order_no,
            "notifyUrl"         =>      $callBackUrl,
            "number"            =>      $charge_account,
            "operator"            =>      7
        ];

        $data1 = "";

        foreach($data as $k => $v)
        {
            $data1 .= $k."=".$v."&";
        }
        $data1 .= "key=".$key;
        $data['sign'] = strtoupper(md5($data1));

        $res = $this->curlPost($url,json_encode($data,JSON_UNESCAPED_SLASHES));
        return $res;
    }

    public function curlPost($url,$post_data = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json; charset=UTF-8'));
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}
